package com.own.business.comment.controller.manage;

import com.own.aop.permission.PermissionModule;
import com.own.aop.permission.PermissionOperation;
import com.own.business.comment.business.service.CommentService;
import com.own.business.comment.common.constant.ConstantModule;
import com.own.business.comment.entity.dto.CommentDto;
import com.own.business.comment.entity.map.CommentMapVo;
import com.own.business.comment.entity.query.CommentQuery;
import com.own.business.comment.entity.vo.CommentVo;
import com.own.component.common.base.controller.BaseController;
import com.own.component.common.model.PageModel;
import com.own.component.common.model.R;
import com.own.component.common.model.ResultModel;
import com.own.component.log.operation.aop.LogOperationAop;
import com.own.component.log.operation.aop.LogOperationModuleAop;
import com.own.component.store.core.confirm.aop.ConfirmAop;
import com.own.constant.ConstantApiPrefix;
import com.own.constant.ConstantBeanSuffix;
import com.own.constant.ConstantPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * CommentUserController
 *
 * @author chenxueli
 * @date 2023-07-03 14:15:52
 */
@Tag(name = "评论" + ConstantBeanSuffix.MANAGE_CLIENT + ConstantBeanSuffix.CONTROLLER)
@RestController
@PermissionModule(ConstantModule.COMMENT)
@LogOperationModuleAop("评论" + ConstantBeanSuffix.MANAGE_CLIENT)
@RequestMapping(ConstantApiPrefix.BASE_API_V1_PREFIX + ConstantApiPrefix.MANAGE_CLIENT + "comment")
public class ManageCommentController extends BaseController {

    @Resource
    private CommentService commentService;

    @LogOperationAop
    @Operation(summary = "查询所有信息")
    @PostMapping("/all")
    public ResultModel<List<CommentVo>> all(
            @RequestBody CommentQuery query
    ) {
        var pageModel = commentService.all(query);
        return R.success(pageModel);
    }

    @LogOperationAop
    @PermissionOperation(ConstantPermission.SEARCH)
    @Operation(summary = "分页查询信息")
    @PostMapping("/page")
    public ResultModel<PageModel<CommentVo>> page(
            @RequestBody CommentQuery query
    ) {
        var pageModel = commentService.page(query);
        return R.success(pageModel);
    }

    @LogOperationAop
    @PermissionOperation(ConstantPermission.SEARCH)
    @Operation(summary = "map查询信息")
    @PostMapping("/map")
    public ResultModel<List<CommentMapVo>> map(
            @RequestBody CommentQuery query
    ) {
        var list = commentService.map(query);
        return R.success(list);
    }

    @LogOperationAop
    @PermissionOperation(ConstantPermission.ADD)
    @Operation(summary = "添加信息")
    @PostMapping("/")
    public ResultModel<CommentVo> add(
            @Validated @RequestBody CommentDto dto
    ) {
        dto.check();
        var updateModel = commentService.add(dto.toBo());
        return R.success(updateModel);
    }

    @LogOperationAop
    @PermissionOperation(ConstantPermission.UPDATE)
    @Operation(summary = "修改信息")
    @PutMapping("/{commentId}")
    public ResultModel<CommentVo> update(
            @Parameter(description = "主键id", required = true) @PathVariable Long commentId,
            @Validated @RequestBody CommentDto dto
    ) {
        dto.check();
        var updateModel = commentService.update(commentId, dto.toBo());
        return R.success(updateModel);
    }

    @LogOperationAop
    @PermissionOperation(ConstantPermission.DELETE)
    @ConfirmAop(message = "确认删除该项目？（注意：删除之后无法找回数据，且该项目所有资料会被删除）")
    @Operation(summary = "删除信息")
    @DeleteMapping("/{commentId}")
    public ResultModel<Boolean> delete(
            @Parameter(description = "主键id", required = true) @PathVariable Long commentId
    ) {
        var updateModel = commentService.delete(commentId);
        return R.success(updateModel);
    }

    @LogOperationAop
    @PermissionOperation(ConstantPermission.VIEW)
    @Operation(summary = "根据id获取信息")
    @GetMapping("/{commentId}")
    public ResultModel<CommentVo> getById(
            @Parameter(description = "主键id", required = true) @PathVariable Long commentId
    ) {
        var vo = commentService.getById(commentId);
        return R.success(vo);
    }

}
